<?php
class TeacherController extends Zend_Controller_Action
{
	
    public function init()
    {
    	Zend_Loader::loadClass('LoginDefaultModel');
    	Zend_Loader::loadClass('SubjectsModel');
    	Zend_Loader::loadClass('TeacherModel');
    	Zend_Loader::loadClass('AdminModel');
    	Zend_Loader::loadClass('TeacherModel');
    	Zend_Loader::loadClass('LoginModel');
    	Zend_Loader::loadClass('StudentsModel');
    	Zend_Loader::loadClass('InstitutesModel');
    	Zend_Loader::loadClass('Zend_Session');
    	Zend_Loader::loadClass('Zend_Session_Namespace');
        Zend_Loader::loadClass('PointModel');
        Zend_Loader::loadClass('ExercisesModel');
    }
    
    public function preDispatch(){
    	
    	/*
    	 * Load template
    	*/
    	$layoutPath = APPLICATION_PATH  . '/templates/default/default';
    	$option = array (	'layout' 		=> 'index',
    			'layoutPath' 	=> $layoutPath );
    	Zend_Layout::startMvc ( $option );
    	
    	$this->view->module = $this->_request->getModuleName();
    	$this->view->controller =  $this->_request->getControllerName();
    	$this->view->action =  $this->_request->getActionName();
    	$lgNamespace = new Zend_Session_Namespace('enrol_teacher');
    	$auth = Zend_Auth::getInstance();
        if(($lgNamespace->enrol_teacher !=1) || !($auth->hasIdentity())){
            echo "Not enrol";die;
        }
    }
    
    function editProfileAction(){
    	$model= new TeacherModel();
    	if(($_REQUEST['id']!='')&&$this->_request->isPost()){
    	//	echo 12;die;
    		$id= $_REQUEST['id'];
    		$name= $this->_request->getParam('name');
    		$birthday= $this->_request->getParam('birthday');
    		$phone= $this->_request->getParam('phone');
    		$institutes= $this->_request->getParam('institutes');
    		$username= $this->_request->getParam('username');
    		$password= base64_encode($this->_request->getParam('password')) ;
    		$email= $this->_request->getParam('email');    	
    	
    		$data= array(
    				'phone'=>$phone,
    				'username'=>$username,
    				'password'=>$password,
    				'email'=>$email,
    		);
    	
    	
    	
    		if($username!='' && $password!='' ){
    			$model->update($data,"id=".$id);
    		//	$this->_redirect('/admin/teacher');
    			$this->view->success = 1;
    	
    		}else{
    			$this->view->warning = 1;
    		}
    	
    	}
    	
    	$dt = new InstitutesModel();
    	$this->view->all_institutes = InstitutesModel::institutes();
    	if($_REQUEST['id']!=''){
    		$id= $_REQUEST['id'];
    		$this->view->data= $model->getDbbyID($id);
    	}
    }

    public function exportPointPdfAction(){
        try {
            $db = Zend_Registry::get('connectDB');
        //  $auth=new libcommon();
            //Tao trang PDF
            $pdfDoc = new Zend_Pdf();
            //Tao trang zise A4
            $pdfPage = $pdfDoc->newPage(Zend_Pdf_Page::SIZE_A4);
            //Tao font cho trang in
            $font = Zend_Pdf_Font::fontWithPath('font/tahoma.ttf');
            
            $nameSubject = "Bảng điểm môn học: ".$this->_request->getParam('name-subject');
            $auth = Zend_Auth::getInstance();
            $user = $auth->getIdentity();
            $teacherName = "Giáo viên: ".$user->name;

            $idSubject = $this->_request->getParam('id-subject');
            $codeSj = "Mã môn học: ".SubjectsModel::getCodeSubject($idSubject);

            $e_id = $this->_request->getParam('e_id');
            $nameEx= ExercisesModel::getNameExById($e_id);
            $nameEx= "Bảng điểm bài tập: ".$nameEx;
    
            //----------------------PHAN THONG TIN TRUONG-----------------
            $pdfPage->setFont($font, 13)
                    ->drawText('TRƯỜNG ĐẠI HỌC BÁCH KHOA HÀ NỘI', 20, 820, 'UTF-8');
            $pdfPage->setFont($font, 12)
                    ->drawText($nameSubject, 20, 800, 'UTF-8');
            $pdfPage->setFont($font, 12)
                    ->drawText($teacherName, 20, 780, 'UTF-8');
            $pdfPage->setFont($font, 12)
                    ->drawText($codeSj, 20, 760, 'UTF-8');
            $pdfPage->setFont($font, 18)
                    ->drawText($nameEx, 170, 730, 'UTF-8');
           
            //-----------PHAN THONG TIN CTIET DIEM------------
            $pdfPage->setFont($font, 11)
                    ->drawText('STT', 80, 690, 'UTF-8');
            $pdfPage->setFont($font, 11)
                    ->drawText('Tên sinh viên', 170, 690, 'UTF-8');
            $pdfPage->setFont($font, 11)
                    ->drawText('SHSV', 297, 690, 'UTF-8');
            $pdfPage->setFont($font, 11)
                    ->drawText('Điểm', 366, 690, 'UTF-8');
            $pdfPage->setFont($font, 11)
                    ->drawText('Bài tập', 441, 690, 'UTF-8');
            
            $infoExport = PointModel::getPointByIdExercises($e_id);
            $gi=0;
            $i=1;
            $tongTien=0;
            
            foreach ($infoExport as $key => $kqCTiet) {
                $pdfPage->setFont($font, 10)
                        ->drawText($i , 86, 670- $gi, 'UTF-8');
                
                $pdfPage->setFont($font, 10)
                        ->drawText($kqCTiet['name_sv'], 170, 670 - $gi, 'UTF-8');
                
                $pdfPage->setFont($font, 10)
                        ->drawText($kqCTiet['shsv_sv'], 300, 670 - $gi, 'UTF-8');

                $pdfPage->setFont($font, 10)
                        ->drawText($kqCTiet['point'], 375, 670 - $gi, 'UTF-8');

                $pdfPage->setFont($font, 10)
                        ->drawText($kqCTiet['e_name'], 440, 670 - $gi, 'UTF-8');
                $gi+=25;
                $i++;
            }
            
            //=======================VE TABLE TRONG FILE PDF======================
            $countRow= count($infoExport);
            //-------------------LAP VE CAC COT DUNG--------------------
            $tn=0;
            $count=$countRow+3;
            $diemdb_x1=65;
            $diemdb_y1=710;
            $diemkt_x2=515;
            $diemkt_y2=710;
            $pdfPage->setLineWidth(0.5);
            $pdfPage->drawLine($diemdb_x1, $diemdb_y1, $diemkt_x2, $diemkt_y2);
            for($i=0;$i<$count;$i++){
                //duong ngang
                $pdfPage->setLineWidth(0.5);
                //------Toa do trong file: tinh tu tren xuong se giam dan
                //------Toa do ve: diem bat dau(x1,y1) ---> diem ket thuc(x2,y2)
                //------cap nhat lai tung do cua 2 diem bat dau va ket thuc
                $diemdb_y1=710-$tn;
                $diemkt_y2=710-$tn;
                $pdfPage->drawLine($diemdb_x1, $diemdb_y1, $diemkt_x2, $diemkt_y2);
                $tn+=25;
            }
            //-------------------LAP VE CAC COT DUNG--------------------
            $td=0;
            for($i=0;$i<7;$i++){
                //cot dung
                switch($i){
                    case 0:
                        $pdfPage->setLineWidth(0.5);
                        $pdfPage->drawLine(65+$td, 710, 65+$td, $diemdb_y1);
                        break;
                    case 1:
                        $pdfPage->setLineWidth(0.5);
                        $pdfPage->drawLine(85+$td, 710, 85+$td, $diemdb_y1+25);
                        break;
                    case 2:
                        $pdfPage->setLineWidth(0.5);
                        $pdfPage->drawLine(230+$td, 710, 230+$td, $diemdb_y1+25);
                        break;
                    case 3:
                        $pdfPage->setLineWidth(0.5);
                        $pdfPage->drawLine(260+$td, 710, 260+$td, $diemdb_y1+25);
                        break;
                    case 4:
                        $pdfPage->setLineWidth(0.5);
                        $pdfPage->drawLine(300+$td, 710, 300+$td, $diemdb_y1);
                        break;
                    case 5:
                        $pdfPage->setLineWidth(0.5);
                        $pdfPage->drawLine(365+$td, 710, 365+$td, $diemdb_y1);
                        break;
                    
                }
                $td+=30;
            }
            //----------------------LUU FILE PDF-----------------------
            
            $pdfDoc->pages[] = $pdfPage;
           /*
            *  Đổi tên file.
            */
            $chars = "0123456789abcdefghijklmnoepqrstxyz";
            $size = strlen( $chars );
            $str = date("H_i_s_d_m_Y")."_";
            for( $i = 0; $i < 5; $i++ ) {
                $str .= $chars[ rand( 0, $size - 1 ) ];
            }
            $pdfnamefile = "file/pdf/".$str.".pdf";
            $pdfDoc->save($pdfnamefile);
            $this->_helper->layout->disableLayout();
            $this->_helper->viewRenderer->setNoRender();
            
            // DOWNLOAD FILE

            $fullPath = $_SERVER['DOCUMENT_ROOT'].DOMAIN."/".$pdfnamefile; // change the path to fit your websites document structure
            if ($fd = fopen ($fullPath, "r")) {
                $fsize = filesize($fullPath);
                $path_parts = pathinfo($fullPath);
                $ext = strtolower($path_parts["extension"]);
                switch ($ext) {
                    case "pdf":
                    header("Content-type: application/pdf"); // add here more headers for diff. extensions
                    header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
                    break;
                    default;
                    header("Content-type: application/octet-stream");
                    header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
                }
                header("Content-length: $fsize");
                header("Cache-control: private"); //use this to open files directly
                while(!feof($fd)) {
                    $buffer = fread($fd, 2048);
                    echo $buffer;
                }
            }
            fclose ($fd);
            exit;
        }
        catch (Exception $e) {
            echo "No data";die;
        }
        
    }

    public function exportPointSubjectPdfAction(){
        try {
            $db = Zend_Registry::get('connectDB');
        //  $auth=new libcommon();
            //Tao trang PDF
            $pdfDoc = new Zend_Pdf();
            //Tao trang zise A4
            $pdfPage = $pdfDoc->newPage(Zend_Pdf_Page::SIZE_A4);
            //Tao font cho trang in
            $font = Zend_Pdf_Font::fontWithPath('font/tahoma.ttf');
            
            $nameSubject = "Bảng điểm môn học: ".$this->_request->getParam('name-subject');
            $auth = Zend_Auth::getInstance();
            $user = $auth->getIdentity();
            $teacherName = "Giáo viên: ".$user->name;

            $idSubject = $this->_request->getParam('id-subject');
            $codeSj = "Mã môn học: ".SubjectsModel::getCodeSubject($idSubject);

    
            //----------------------PHAN THONG TIN TRUONG-----------------
            $pdfPage->setFont($font, 13)
                    ->drawText('TRƯỜNG ĐẠI HỌC BÁCH KHOA HÀ NỘI', 20, 820, 'UTF-8');
            $pdfPage->setFont($font, 12)
                    ->drawText($nameSubject, 20, 800, 'UTF-8');
            $pdfPage->setFont($font, 12)
                    ->drawText($teacherName, 20, 780, 'UTF-8');
            $pdfPage->setFont($font, 12)
                    ->drawText($codeSj, 20, 760, 'UTF-8');
            $pdfPage->setFont($font, 18)
                    ->drawText("Bảng điểm môn học", 210, 730, 'UTF-8');

           
            //-----------PHAN THONG TIN CTIET DIEM------------
            $pdfPage->setFont($font, 11)
                    ->drawText('STT', 80, 690, 'UTF-8');
            $pdfPage->setFont($font, 11)
                    ->drawText('Tên sinh viên', 170, 690, 'UTF-8');
            $pdfPage->setFont($font, 11)
                    ->drawText('SHSV', 297, 690, 'UTF-8');
            $pdfPage->setFont($font, 11)
                    ->drawText('Lớp', 366, 690, 'UTF-8');
            $pdfPage->setFont($font, 11)
                    ->drawText('Điểm', 441, 690, 'UTF-8');
            
            $typePoint = $this->_request->getParam('type-point');
            $infoExport = PointModel::getPointByIdSubjectAndInfoExtend($idSubject,$typePoint);
       
            $gi=0;
            $i=1;
            $tongTien=0;
            
            foreach ($infoExport as $key => $kqCTiet) {
                $pdfPage->setFont($font, 10)
                        ->drawText($i , 86, 670- $gi, 'UTF-8');
                
                $pdfPage->setFont($font, 10)
                        ->drawText($kqCTiet['name_sv'], 170, 670 - $gi, 'UTF-8');
                
                $pdfPage->setFont($font, 10)
                        ->drawText($kqCTiet['shsv_sv'], 300, 670 - $gi, 'UTF-8');

                $pdfPage->setFont($font, 10)
                        ->drawText($kqCTiet['class'], 375, 670 - $gi, 'UTF-8');

                $pdfPage->setFont($font, 10)
                        ->drawText($kqCTiet['point'], 440, 670 - $gi, 'UTF-8');
                $gi+=25;
                $i++;
            }
            
            //=======================VE TABLE TRONG FILE PDF======================
            $countRow= count($infoExport);
            //-------------------LAP VE CAC COT DUNG--------------------
            $tn=0;
            $count=$countRow+3;
            $diemdb_x1=65;
            $diemdb_y1=710;
            $diemkt_x2=515;
            $diemkt_y2=710;
            $pdfPage->setLineWidth(0.5);
            $pdfPage->drawLine($diemdb_x1, $diemdb_y1, $diemkt_x2, $diemkt_y2);
            for($i=0;$i<$count;$i++){
                //duong ngang
                $pdfPage->setLineWidth(0.5);
                //------Toa do trong file: tinh tu tren xuong se giam dan
                //------Toa do ve: diem bat dau(x1,y1) ---> diem ket thuc(x2,y2)
                //------cap nhat lai tung do cua 2 diem bat dau va ket thuc
                $diemdb_y1=710-$tn;
                $diemkt_y2=710-$tn;
                $pdfPage->drawLine($diemdb_x1, $diemdb_y1, $diemkt_x2, $diemkt_y2);
                $tn+=25;
            }
            //-------------------LAP VE CAC COT DUNG--------------------
            $td=0;
            for($i=0;$i<7;$i++){
                //cot dung
                switch($i){
                    case 0:
                        $pdfPage->setLineWidth(0.5);
                        $pdfPage->drawLine(65+$td, 710, 65+$td, $diemdb_y1);
                        break;
                    case 1:
                        $pdfPage->setLineWidth(0.5);
                        $pdfPage->drawLine(85+$td, 710, 85+$td, $diemdb_y1+25);
                        break;
                    case 2:
                        $pdfPage->setLineWidth(0.5);
                        $pdfPage->drawLine(230+$td, 710, 230+$td, $diemdb_y1+25);
                        break;
                    case 3:
                        $pdfPage->setLineWidth(0.5);
                        $pdfPage->drawLine(260+$td, 710, 260+$td, $diemdb_y1+25);
                        break;
                    case 4:
                        $pdfPage->setLineWidth(0.5);
                        $pdfPage->drawLine(300+$td, 710, 300+$td, $diemdb_y1);
                        break;
                    case 5:
                        $pdfPage->setLineWidth(0.5);
                        $pdfPage->drawLine(365+$td, 710, 365+$td, $diemdb_y1);
                        break;
                    
                }
                $td+=30;
            }
            //----------------------LUU FILE PDF-----------------------
            
            $pdfDoc->pages[] = $pdfPage;
           /*
            *  Đổi tên file.
            */
            $chars = "0123456789abcdefghijklmnoepqrstxyz";
            $size = strlen( $chars );
            $str = date("H_i_s_d_m_Y")."_";
            for( $i = 0; $i < 5; $i++ ) {
                $str .= $chars[ rand( 0, $size - 1 ) ];
            }
            $pdfnamefile = "file/pdf/".$str.".pdf";
            $pdfDoc->save($pdfnamefile);
            $this->_helper->layout->disableLayout();
            $this->_helper->viewRenderer->setNoRender();

            // DOWNLOAD FILE

            $fullPath = $_SERVER['DOCUMENT_ROOT'].DOMAIN."/".$pdfnamefile; // change the path to fit your websites document structure
            if ($fd = fopen ($fullPath, "r")) {
                $fsize = filesize($fullPath);
                $path_parts = pathinfo($fullPath);
                $ext = strtolower($path_parts["extension"]);
                switch ($ext) {
                    case "pdf":
                    header("Content-type: application/pdf"); // add here more headers for diff. extensions
                    header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download
                    break;
                    default;
                    header("Content-type: application/octet-stream");
                    header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
                }
                header("Content-length: $fsize");
                header("Cache-control: private"); //use this to open files directly
                while(!feof($fd)) {
                    $buffer = fread($fd, 2048);
                    echo $buffer;
                }
            }
            fclose ($fd);
            exit;
        }
        catch (Exception $e) {
            echo "No data";die;
        }
        
    }


}